package com.uxhuanche.ui.base;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uxhuanche.ui.R;


/**
 * @author by Administrator on 2015/6/15.
 */
public class NavigateBar extends LinearLayout implements View.OnClickListener {
    Context context;

    ImageView ivIcon;//左边的Icon
    TextView tvLeftTitle;//左标题
    TextView tvCenterTitle;//标题

    ImageView ivIcons;//左边副icon

    TextView tvOperateTitle;//右边的title
    ImageView ivOperateIcon;//右边Icon
    ImageView ivOperateIconSecond;

    //click区域
    FrameLayout vIcon;
    FrameLayout vOperate;
    FrameLayout vOperate_second;
    FrameLayout vIcons;

    boolean retEnable;
    OnOperateClickListener opeListener;
    OnOperateClickListenerSecond opeListenerSecond;
    OnIconClickListener iconListener;
    OnIconClickListeners iconListeners;

    public NavigateBar(Context context) {
        super(context);
        this.context = context;
    }

    public NavigateBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

        addView(LayoutInflater.from(context).inflate(R.layout.view_navigate_bar, null),
                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        ivIcon = (ImageView) findViewById(R.id.ivIcon);
        ivIcons = (ImageView) findViewById(R.id.ivIcons);
        tvLeftTitle = (TextView) findViewById(R.id.tvLeftTitle);
        tvCenterTitle = (TextView) findViewById(R.id.tvCenterTitle);
        tvOperateTitle = (TextView) findViewById(R.id.tvOperate);
        ivOperateIcon = (ImageView) findViewById(R.id.ivOperate);
        ivOperateIconSecond = (ImageView) findViewById(R.id.ivOperate_second);

        vIcon = (FrameLayout) findViewById(R.id.vIcon);
        vIcons = (FrameLayout) findViewById(R.id.vIcons);
        vOperate = (FrameLayout) findViewById(R.id.vOperate);
        vIcon.setOnClickListener(this);
        vIcons.setOnClickListener(this);
        vOperate.setOnClickListener(this);
        vOperate_second = (FrameLayout) findViewById(R.id.vOperate_second);
        vOperate_second.setOnClickListener(this);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NavigateBar);
        retEnable = a.getBoolean(R.styleable.NavigateBar_navigate_return_enable, true);
        setIcon(a.getResourceId(R.styleable.NavigateBar_navigate_bar_icon, 0));
        setIcons(a.getResourceId(R.styleable.NavigateBar_navigate_bar_icons, 0));
        setLeftTitle(a.getString(R.styleable.NavigateBar_navigate_bar_left_title));
        setCenterTitle(a.getString(R.styleable.NavigateBar_navigate_bar_center_title));
        setOperateTitle(a.getString(R.styleable.NavigateBar_navigate_bar_operate_title));
        setOperateTitleColor(a.getResourceId(R.styleable.NavigateBar_navigate_bar_operate_title_color, -1));
        setOperateIcon(a.getResourceId(R.styleable.NavigateBar_navigate_bar_operate_icon, 0));
        setOperateIconSecond(a.getResourceId(R.styleable.NavigateBar_navigate_bar_operate_icon_second, 0));
        this.setMiddleTextFakeBold();
        a.recycle();
    }

    public void setIcon(int resId) {
        ivIcon.setImageResource(resId);
    }

    public void setIconVisible(int v) {
        vIcons.setVisibility(v);
    }

    public void setAllIconVisible(int v) {
        vIcon.setVisibility(v);
        vIcons.setVisibility(v);
        MarginLayoutParams params = (MarginLayoutParams) tvLeftTitle.getLayoutParams();
        params.leftMargin = DensityUtils.dip2px(getContext(), 16);
        tvLeftTitle.setLayoutParams(params);
    }

    public void setIcons(int resId) {
        ivIcons.setImageResource(resId);
    }

    public void setLeftTitle(String title) {
        tvLeftTitle.setText(title == null ? "" : title);
    }

    public void setCenterTitle(String title) {
        tvCenterTitle.setText(title == null ? "" : title);
    }

    public void setMiddleTextFakeBold() {
        this.tvCenterTitle.getPaint().setFakeBoldText(true);
    }

    public void setOperateTextFackBold() {
        this.tvOperateTitle.getPaint().setFakeBoldText(true);
    }

    public void setOperateTitle(String operateTitle) {
        tvOperateTitle.setText(operateTitle == null ? "" : operateTitle);
    }

    private void setOperateTitleColor(int integer) {
        if (integer > 0) {
            tvOperateTitle.setTextColor(getResColor(tvCenterTitle, integer));
        } else {
            tvOperateTitle.setTextColor(getResColor(tvCenterTitle, R.color.font_tip));
        }
    }

    public void setOperateIcon(int resId) {
        ivOperateIcon.setImageResource(resId);
    }

    public void setOperateIconSecond(int resId) {
        ivOperateIconSecond.setImageResource(resId);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.vIcon) {
            if (iconListener != null) {
                iconListener.OnIconClick(v);
            } else {
                if (retEnable && getContext() instanceof Activity) {
                    ((Activity) getContext()).finish();
                }
            }
        } else if (v.getId() == R.id.vOperate) {
            if (opeListener != null) {
                opeListener.onOperateClick(v);
            }
        } else if (v.getId() == R.id.vIcons) {
            if (iconListeners != null) {
                iconListeners.OnIconClick(v);
            }
        } else if (v.getId() == R.id.vOperate_second) {
            if (opeListenerSecond != null) {
                opeListenerSecond.onOperateClickSecond(v);
            }
        }
    }

    public void setIvOperateIconSecondVisible(boolean v) {
        if (v) {
            vOperate_second.setVisibility(VISIBLE);
        } else {
            vOperate_second.setVisibility(GONE);
        }
    }

    public void setOperateIconVisible(boolean v) {
        if (v) {
            vOperate.setVisibility(VISIBLE);
        } else {
            vOperate.setVisibility(GONE);
        }
    }

    public void setOnOperateClickListener(OnOperateClickListener listener) {
        this.opeListener = listener;
    }

    public void setOnOperateClickListenerSecond(OnOperateClickListenerSecond listenerSecond) {
        opeListenerSecond = listenerSecond;
    }

    public void setOnIconClickListener(OnIconClickListener listener) {
        this.iconListener = listener;
    }

    public void setOnIconClickListeners(OnIconClickListeners listener) {
        this.iconListeners = listener;
    }

    public static interface OnIconClickListeners {
        public void OnIconClick(View view);
    }

    public static interface OnOperateClickListener {
        public void onOperateClick(View view);
    }

    public static interface OnOperateClickListenerSecond {
        public void onOperateClickSecond(View view);
    }

    public static interface OnIconClickListener {
        public void OnIconClick(View view);
    }

    int getResColor(View target, int res) {
        try {
            if (target != null && res > 0) {
                return target.getResources().getColor(res);
            }
        } catch (Exception ex) {
            //nothing
        }
        return 0;
    }
}
